#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"


TPosCoor	VideoTPAdjPos[5]= { {40,40}, {640,40}, {40,400}, {640,420}, {340,220} };
TPosCoor	VideoTPAdjInfoPos ={190,140};
int		AudioTPAdjFocus = -1;
TFrame	VideoTPAdjFrame;
//unsigned char calibrate_data [6] = {0xe0,0x03,0x0B,0x00,0xEE};
extern  unsigned char send_data_buf[5];
static TPosCoor	Touch_clbr_pos[5];
static unsigned char touch_clbr_flag ; ///20080217
extern	unsigned char read_info_data[10];
extern  TRect	  MuteInfoPos;
extern  unsigned char mute_status;
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern int TS_KX; ////20080217
extern int TS_KY;
extern int TP_CLBR_C_LX ;//logic x
extern int TP_CLBR_C_LY ;
extern pthread_cond_t dt_cond;



int  TPAdjInfoUpdata (void)
{
 return 0;
}

void Disp_TouchCalibrate (TCanvas *pCanvas,int idx)
{
	char FileName[20];

	g_FillRect( VideoTPAdjFrame.pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_WHITE);		
	
	sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_TPADJ_FOLDER, IMG_VIDEO_TPADJ_FOCUS);
	DrawJpeg(pCanvas, VideoTPAdjPos[idx].x, VideoTPAdjPos[idx].y, FileName);
	
	sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_TPADJ_FOLDER, IMG_VIDEO_TPADJ_INFOCOVER);
	DrawJpeg(pCanvas, VideoTPAdjInfoPos.x, VideoTPAdjInfoPos.y, FileName);	
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);	
	//dt_Present();	
}
/*
static int send_tp_data_out ( int index)
{
	int value=0;
	if ((index < 0) || (index > 2))
		return -1;
		
		switch(index)
				{
					case 1:
						calibrate_data [2] = 0x0B;
						value = TS_KX;
						break;
					case 2:
						calibrate_data [2] = 0x0C;
						value = TS_KY;
						break;
				}		
		calibrate_data [3] = (unsigned char )(0x00 + value);
		calibrate_data [4] = (unsigned char )(0xEE + index + value -1);
		
		if (dev_SPIWrite(calibrate_data,5)==0)
		return 0;	
	return -1;	
}
*/

static int send_data ( int index)
{
	int value=0;
	if ((index < 0) || (index > 2))
		return -1;
		
		switch(index)
				{
					case 1:
						send_data_buf [2] = IDDATA_KX;
						value = TS_KX;
						break;
					case 2:
						send_data_buf [2] = IDDATA_KY;
						value = TS_KY;
						break;
				}	
		send_data_buf[0] =  HEAD;		
		send_data_buf[1] =  LENGTH;
		send_data_buf [3] = (unsigned char )(0x00 + value);
		
		return(send_data_out(send_data_buf,ALL_LENGTH));
			
		return -1;
}


void WriteToText(void)
{
	int 	file;
	int 	buffer[32];	
	
		file = File_Open();	
		
		if (file == -1)
     {
     	printf("open TPdata.txt error!20080304\n");
    	}
        
    buffer[0] = (int)TS_KX;
    buffer[1] = (int)TS_KY;
    buffer[2] = TP_CLBR_C_X;
    buffer[3] = TP_CLBR_C_Y;
    buffer[4] = TP_CLBR_C_LX;
    buffer[5] = TP_CLBR_C_LY;
    if ( !(File_Write (buffer, 24)) )
    	{
    		printf ("write TPdata.txt error!\n");
    	}
    else
    	printf ("TP_CLBR_C_LX is %d,TP_CLBR_C_LY is %d,write TPdata.txt success!\n",TP_CLBR_C_LX,TP_CLBR_C_LY);   
   File_Close ( ); 
   
}





int TouchCalibrate (TPosCoor pos)
{	
	int offer_x = 0,offer_y = 0;
	int temp_x = 0,temp_y = 0;
	long temp_kx,temp_ky;

	switch (touch_clbr_flag)
		{
		case	0:
			temp_x = 60;
			temp_y = 60;
			break;
		case	1:
			temp_x = 660;
			temp_y = 60;
			break;
		case	2:
			temp_x = 60;
			temp_y = 420;
			break;
		case	3:
			temp_x = 660;
			temp_y = 420;
			break;
		case	4:
			temp_x = 360;
			temp_y = 240;
			break;
		}

	 				
	offer_x = ((pos.x-temp_x >0) ? (pos.x-temp_x) : (-1 *(pos.x-temp_x)) ) ;
	offer_y = ((pos.y-temp_y >0) ? (pos.y-temp_y) : (-1 *(pos.y-temp_y)) );
		
	
	
	if ((offer_x < TP_CLBR_OFFER ) && (offer_x < TP_CLBR_OFFER) )
	 {
	 		switch (temp_x)
	 		{
	 			case 60:
	 				if (pos.y <100)
	 					{
	 						Touch_clbr_pos[0].x = pos.x;
	 						Touch_clbr_pos[0].y = pos.y;
	 					}
	 				else
	 					{
	 						Touch_clbr_pos[2].x = pos.x;
	 						Touch_clbr_pos[2].y = pos.y;
	 						TP_CLBR_C_LX = pos.x;
	 						TP_CLBR_C_LY = pos.y;
	 					}
	 				break;
	 			case 660:
	 				if (pos.y <100)
	 					{
	 						Touch_clbr_pos[1].x = pos.x;
	 						Touch_clbr_pos[1].y = pos.y;
	 					}
	 				else
	 					{
	 						Touch_clbr_pos[3].x = pos.x;
	 						Touch_clbr_pos[3].y = pos.y;
	 					}
	 				break;
	 			case 360:	 				
	 						Touch_clbr_pos[4].x = pos.x;
	 						Touch_clbr_pos[4].y = pos.y;	 					
	 				break;
	 		} 	 	
	 	}
	 	else
	 		{
	 			printf ("Too much offer!!\n");
	 			return -1;		
	 		}
	 		
	 	if ( Touch_clbr_pos[4].x != 0)
	 	{		
	 		temp_kx = ((Touch_clbr_pos[1].x-Touch_clbr_pos[0].x)*100/(TP_CLBR_B_X-TP_CLBR_A_X)+(Touch_clbr_pos[3].x-Touch_clbr_pos[2].x)*100/(TP_CLBR_D_X-TP_CLBR_C_X))/2;
	 		temp_ky = ((Touch_clbr_pos[2].y-Touch_clbr_pos[0].y)*100/(TP_CLBR_C_Y-TP_CLBR_A_Y)+(Touch_clbr_pos[3].y-Touch_clbr_pos[1].y)*100/(TP_CLBR_D_Y-TP_CLBR_B_Y))/2;
	 		temp_x = Touch_clbr_pos[2].x + (temp_kx*(TP_CLBR_E_X - TP_CLBR_C_X))/100;
	 		temp_y = Touch_clbr_pos[2].y + (temp_ky*(TP_CLBR_E_Y - TP_CLBR_C_Y))/100;
	 		
			offer_x = (Touch_clbr_pos[4].x-temp_x >0) ? (Touch_clbr_pos[4].x-temp_x) : (-1 *(Touch_clbr_pos[4].x-temp_x)) ;
			offer_y = (Touch_clbr_pos[4].y-temp_y >0) ? (Touch_clbr_pos[4].y-temp_y) : (-1 *(Touch_clbr_pos[4].y-temp_y)) ;

			if ((offer_x < TP_CLBR_OFFER ) && (offer_x < TP_CLBR_OFFER) )
			{
				printf ("  center is calulate ok!!\n");
				printf ("temp_kx: %ld ,temp_ky is: %ld temp_x: %d,temp_y: %d\n",temp_kx,temp_ky,temp_x,temp_y);
				TS_KX = (temp_kx > TP_KXY_LI) ? temp_kx : TS_KX;
					
				if ( send_data (1))
					printf ("write failure !\n");	
					
				TS_KY = (temp_ky > TP_KXY_LI) ? temp_ky : TS_KY;
				memset(Touch_clbr_pos, 0, sizeof(Touch_clbr_pos));	
				touch_clbr_flag = 0;				
				if ( send_data (2))
					printf ("write failure !\n");	
				WriteToText();				
				return 1;
			}	
			else
				{
				printf ("  center is calulate error!!\n");		
				touch_clbr_flag = 0;//////////////maybe can be changed.20080708
				return -1;		
				}			
		}
		return 0;	
}


/*int TouchCalibrate (TPosCoor pos)
{	
	int offer_x,offer_y;
	int temp_x,temp_y;
	long temp_kx,temp_ky;
	
	temp_x = (pos.x <100) ? 60:660;
	temp_y = (pos.y <100) ? 60:420;			
	
	if ( (Touch_clbr_pos[0].x != 0) && (pos.x>100 ) && (pos.x<400 ))
	{
		temp_x = 360;
		temp_y = 240;			
	}
	 				
	offer_x = ((pos.x-temp_x >0) ? (pos.x-temp_x) : (-1 *(pos.x-temp_x)) ) ;
	offer_y = ((pos.y-temp_y >0) ? (pos.y-temp_y) : (-1 *(pos.y-temp_y)) );
		
	
	
	if ((offer_x < TP_CLBR_OFFER ) && (offer_x < TP_CLBR_OFFER) )
	 {
	 		switch (temp_x)
	 		{
	 			case 60:
	 				if (pos.y <100)
	 					{
	 						Touch_clbr_pos[0].x = pos.x;
	 						Touch_clbr_pos[0].y = pos.y;
	 					}
	 				else
	 					{
	 						Touch_clbr_pos[2].x = pos.x;
	 						Touch_clbr_pos[2].y = pos.y;
	 						TP_CLBR_C_LX = pos.x;
	 						TP_CLBR_C_LY = pos.y;
	 					}
	 				break;
	 			case 660:
	 				if (pos.y <100)
	 					{
	 						Touch_clbr_pos[1].x = pos.x;
	 						Touch_clbr_pos[1].y = pos.y;
	 					}
	 				else
	 					{
	 						Touch_clbr_pos[3].x = pos.x;
	 						Touch_clbr_pos[3].y = pos.y;
	 					}
	 				break;
	 			case 360:	 				
	 						Touch_clbr_pos[4].x = pos.x;
	 						Touch_clbr_pos[4].y = pos.y;	 					
	 				break;
	 		} 	 	
	 	}
	 	else
	 		{
	 			printf ("Too much offer!!\n");
	 			return 0;		
	 		}
	 		
	 	if ( Touch_clbr_pos[4].x != 0)
	 	{		
	 		temp_kx = ((Touch_clbr_pos[1].x-Touch_clbr_pos[0].x)*100/(TP_CLBR_B_X-TP_CLBR_A_X)+(Touch_clbr_pos[3].x-Touch_clbr_pos[2].x)*100/(TP_CLBR_D_X-TP_CLBR_C_X))/2;
	 		temp_ky = ((Touch_clbr_pos[2].y-Touch_clbr_pos[0].y)*100/(TP_CLBR_C_Y-TP_CLBR_A_Y)+(Touch_clbr_pos[3].y-Touch_clbr_pos[1].y)*100/(TP_CLBR_D_Y-TP_CLBR_B_Y))/2;
	 		temp_x = Touch_clbr_pos[2].x + (temp_kx*(TP_CLBR_E_X - TP_CLBR_C_X))/100;
	 		temp_y = Touch_clbr_pos[2].y + (temp_ky*(TP_CLBR_E_Y - TP_CLBR_C_Y))/100;
	 		
			offer_x = (Touch_clbr_pos[4].x-temp_x >0) ? (Touch_clbr_pos[4].x-temp_x) : (-1 *(Touch_clbr_pos[4].x-temp_x)) ;
			offer_y = (Touch_clbr_pos[4].y-temp_y >0) ? (Touch_clbr_pos[4].y-temp_y) : (-1 *(Touch_clbr_pos[4].y-temp_y)) ;

			if ((offer_x < TP_CLBR_OFFER ) && (offer_x < TP_CLBR_OFFER) )
			{
				printf ("  center is calulate ok!!\n");
				printf ("temp_kx: %d ,temp_ky is: %d temp_x: %d,temp_y: %d\n",temp_kx,temp_ky,temp_x,temp_y);
				TS_KX = (temp_kx > TP_KXY_LI) ? temp_kx : TS_KX;
					
				if ( send_tp_data_out (1))
					printf ("write failure !\n");	
					
				TS_KY = (temp_ky > TP_KXY_LI) ? temp_ky : TS_KY;
				memset(Touch_clbr_pos, 0, sizeof(Touch_clbr_pos));	
				touch_clbr_flag = 0;
				MenuVideoDraw( TRUE );				
				if ( send_tp_data_out (2))
					printf ("write failure !\n");	
				WriteToText();
				return 1;
			}	
			else
				{
				printf ("  center is calulate error!!\n");						
				}			
		}
		return 0;	
}
*/


int VideoTPAdjGetTouchID( TPosCoor pos )
{
	char FileName[20];
	int GetJpegIdx;	
	TRect Rect;
	TJpeg pJpeg;
	int rc;

	GetJpegIdx = IMG_VIDEO_TPADJ_FOCUS;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_TPADJ_FOLDER, GetJpegIdx);
	pJpeg = GetJpegPtr(FileName);

	Rect.left = VideoTPAdjPos[touch_clbr_flag].x ;//- TP_CLBR_OFFER;
	Rect.top = VideoTPAdjPos[touch_clbr_flag].y;
	Rect.right= VideoTPAdjPos[touch_clbr_flag].x +pJpeg.Width;
	Rect.bottom= VideoTPAdjPos[touch_clbr_flag].y+pJpeg.Height;

	rc = TouchCalibrate(pos);
	if(rc == -1)//ut_PtInRect( pos, Rect ) ) //all is success.
		return -1;
	if (rc ==1)
		{
			//touch_clbr_flag = 0;
			MenuVideoInit();	
			printf ("in VideoTPAdjGetTouchID\n");
			return -1;
		}
	else
		{
			touch_clbr_flag++; 
			//Disp_TouchCalibrate(VideoTPAdjFrame.pCanvas,touch_clbr_flag);
		}
	return 0;
	
}


void VideoTPAdjDraw( BOOL bDrawAll )
{
	printf (" in VideoTPAdjDraw ~~\n");
	g_FillRect( VideoTPAdjFrame.pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_WHITE);		
	touch_clbr_flag =0;
	memset(Touch_clbr_pos, 0, sizeof(Touch_clbr_pos));	
	TS_KX = 100;
	TS_KY = 100;
	Disp_TouchCalibrate (VideoTPAdjFrame.pCanvas,touch_clbr_flag);

	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	
}


void TPAdjInit()
{
	ir_SetCurrentID( IR_VIDEO_TPADJ );
	
	frm_Create( &VideoTPAdjFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );		
	VideoTPAdjDraw( TRUE );		
}


/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	
	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:	
							switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
											MenuRadioInit();
										break;									
									case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuCdInit();
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:
											MenuMediaInit();
										break;
									
									case 	ID_SCREEN_NAVI:
										MenuNaviInit();
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										//MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}
									rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;											
						break;
						
					case	ID_KEY_STATUS:						
						break;
						
					case	ID_SCREEN_INFO:
						if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
								{
									MenuVideoInit();
								}
								break;
						
					case	ID_KEYPAD:	
						switch (read_info_data[3])
							{
									case 	ID_MUTE:										
									case	ID_DMUTE:	
										MenuVideoDraw( TRUE );
										break;									
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
										break;
										
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
										
									case 	ID_FMAM:
										MenuRadioInit();
										break;
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();								
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}	
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
						break;
					case 	ID_BAND_SEL:
							MenuRadioInit();
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
						break;	
						
					default:	
							rc = -1;						
						}
	return 0;	
}





void TouchPanelAdjInput(UInt32 key)
{
		int flag;
	
		if ( key == IRC_READ_INFO)
		{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}			
		}
		
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( AudioTPAdjFocus = VideoTPAdjGetTouchID( pos )) != -1 )
				{
					Disp_TouchCalibrate(VideoTPAdjFrame.pCanvas,touch_clbr_flag);					
					//dt_Present_part(VideoDispAdjTouchRect [VideoDispAdjFocus]);
					//if ( send_data_out(VideoDispAdjFocus))
					//printf ("write failure !\n");	
					//MenuVideoBtnEnter( MenuVideoFocus );
					AudioTPAdjFocus = -1;
				}
			}
		}
/*		else
		{
			if( ( focus = VideoTPAdjGetTouchID( pos ) ) == VideoDispAdjFocus )
				MenuVideoBtnEnter( VideoDispAdjFocus );
			else if( VideoDispAdjFocus != -1 )
			{
				VideoDispAdjDrawItem( VideoDispAdjFrame.pCanvas, VideoDispAdjFocus, FALSE );
				//dtpresent_flag++;		
				//pthread_cond_signal(&dt_cond);
				//dt_Present();
				dt_Present_part(VideoDispAdjTouchRect [VideoDispAdjFocus]);
			}
			VideoDispAdjFocus = -1;
		}
		*/
		LastKey = key;//hangsheng add 071126
	}
}
